import java.io.IOException;
import java.sql.*;

public class Servlet extends javax.servlet.http.HttpServlet {
    protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
        Connection conn = null;
        try {
            // 加载驱动类
            Class.forName("org.hsqldb.jdbcDriver");
            // 建立连接
            conn = DriverManager.getConnection("jdbc:hsqldb:hsql://localhost/",
                    "SA", "");
            // 创建Statement对象
            Statement stmt = conn.createStatement();
            ResultSet rs=stmt.executeQuery("SELECT* FROM DATA WHERE ID='admin' and PASSWORD='123'" );
            if(!rs.next()){ stmt.execute("INSERT INTO DATA (ID,PASSWORD)VALUES('admin','123')");}
            rs.close();
           // stmt.execute("DELETE FROM DATA WHERE ID='admin' and PASSWORD='123'");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            System.out.print(e);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.print(e);
        }
        String id=request.getParameter("id");
        String password=request.getParameter("password");
        if(id!=""&&password!=""){
            try {
                Statement stmt2 = conn.createStatement();
                ResultSet rs2=stmt2.executeQuery("SELECT* FROM DATA WHERE ID="+"\'"+id+"\'"+" and PASSWORD="+"\'"+password+"\'");
                if(rs2.next()){
                    response.sendRedirect("login_suc.jsp");
                }
                else{
                    response.sendRedirect("login_fail.jsp");
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        else{
            response.sendRedirect("login_fail.jsp");
        }
    }

}
